// pages/personalCenter/info/info.js

import { userDetail,file,userUpdate } from '../../../common/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    imageSrc: '',
    cityText: '',
    visible: true,
    visibleText: '',
    cityValue: [],
    obj: {},
    imgPath: '',
    theme: '',
    citys: [
      { label: '男', value: '男' },
      { label: '女', value: '女' },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  // 获取用户头像
  bindchooseavatar(e){
    console.log("=============",e)
    this.uploadFile(e.detail.avatarUrl)
  },
  async uploadFile(e){
    let res =  await file(e);
    console.log('返回的图片', res)
    this.data.imgPath = res.path;
    this.setData({
      imageSrc: res.url
    })
  },
  bindnicknamereview(e){
    console.log('名字', e)
  },
  onColumnChange(e) {
    console.log('picker pick:', e);
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  onCityPicker() {
    this.setData({ cityVisible: true });
  },
  inputChange: function(e){
    console.log('xcz',e)
    this.data.name = e.detail.value;
    this.setData({
      name: e.detail.value
    })
  },

  async getUserDetail(){
    let user = wx.getStorageSync('userInfo');
    let res = await userDetail({id: user.id});
    console.log(res);
    this.data.obj = res;
    console.log(this.data.obj);
    // 为1是显示审核中，为2不显示，为3显示审核失败
    let text = '';
    let show = true;
    let theme = 'info';
    if(res.checkUpdate == 3){
      text = '审核失败';
      theme = 'error'
    }else if(res.checkUpdate == 2){
      show = false;
    }else {
      text = '审核中';
    }
    this.setData({
      imageSrc: res.imageUrl,
      name:res.username,
      cityText: res.gender,
      visible: show,
      visibleText: text,
      theme: theme
    })
  },
  click(e) {
    const { trigger } = e.detail;
    console.log(`click on the ${trigger} area`);
    this.setData({
      visible: false
    })
  },
  async submit(){
    let datas = Object.assign({},this.data.obj)
    // datas.imageUrl = this.data.imageSrc;
  
    datas.image = this.data.imgPath;
    datas.username = this.data.name;
    datas.gender = this.data.cityText;
    console.log(datas)
    let res = await userUpdate(datas);
    wx.showToast({
      title: '修改成功',
      icon: 'none'
    });
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', true);
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 800);
  }
})